Password Generator Using Javascript

Posted on September 28, 2025 by Vishesh Namdev
Python C C++ Javascript Java

Password Generator Project using JavaScript

In this project, we’ll create a Password Generator using JavaScript Classes and Objects. This program will generate a random password that contains at least one lowercase letter, one uppercase letter, and one special character — fulfilling the basic conditions of a secure password.

⚙️ Project Objective

The main goal of this project is to create a password generator that:

  • Generates random passwords of desired length.
  • Ensures the password contains at least one lowercase, one uppercase letter and one special character
  • Uses class and object-oriented design in JavaScript.
  • Code:-

    class Password {
      constructor() {
        console.log("Welcome to Password Generator");
      }
    
      generatePassword(len) {
        if (len < 3) {
          return "Password must be at least 3 characters long!";
        }
    
        let lowercase = "abcdefghijklmnopqrstuvwxyz";
        let uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        let numbers = "0123456789";
        let special = "!@#$%^&*()";
    
        // Ensure at least one of each required type (lower, upper, special)
        let pass = "";
        pass += lowercase[Math.floor(Math.random() * lowercase.length)];
        pass += uppercase[Math.floor(Math.random() * uppercase.length)];
        pass += special[Math.floor(Math.random() * special.length)];
    
        // Fill remaining with all characters
        let allChars = lowercase + uppercase + numbers + special;
        for (let i = pass.length; i < len; i++) {
          pass += allChars[Math.floor(Math.random() * allChars.length)];
        }
    
        // Shuffle to avoid predictable order
        pass = pass.split("").sort(() => Math.random() - 0.5).join("");
        return pass;
      }
    }
    
    // Example
    let p = new Password();
    console.log(p.generatePassword(8));  // Example: "aF@9kd2G"

    Explanation:-

  • The class Password is created to generate random passwords:
  • class Password {
      constructor() {
        console.log("Welcome to Password Generator");
      }
  • The method generatePassword(len) takes the desired password length as input and checks if it’s at least 3:
  • if (len < 3) {
      return "Password must be at least 3 characters long!";
    }
  • Character sets for lowercase, uppercase, numbers, and special characters are defined
  • let lowercase = "abcdefghijklmnopqrstuvwxyz";
    let uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    let numbers = "0123456789";
    let special = "!@#$%^&*()";
  • The password starts by ensuring at least one lowercase, one uppercase, and one special character
  • pass += lowercase[Math.floor(Math.random() * lowercase.length)];
    pass += uppercase[Math.floor(Math.random() * uppercase.length)];
    pass += special[Math.floor(Math.random() * special.length)];
  • The password starts by ensuring at least one lowercase, one uppercase, and one special character
  • pass += lowercase[Math.floor(Math.random() * lowercase.length)];
    pass += uppercase[Math.floor(Math.random() * uppercase.length)];
    pass += special[Math.floor(Math.random() * special.length)];
  • The remaining characters are filled randomly from all character sets
  • let allChars = lowercase + uppercase + numbers + special;
  • The remaining characters are filled randomly from all character sets
  • let allChars = lowercase + uppercase + numbers + special;
  • The characters are shuffled to make the password unpredictable:
  • pass = pass.split("").sort(() => Math.random() - 0.5).join("");
  • Finally, the generated password is returned and printed:
  • return pass;
    Conclusion:-

    This project demonstrates how to use JavaScript Classes and Objects to solve a real-world problem — generating strong passwords. It’s a great exercise in both OOP concepts and string manipulation.
    You can enhance it further by:

  • Adding a password strength meter.
  • Allowing users to choose what types of characters to include.
  • Building a simple UI using HTML & CSS.
  • 📢Important Note📢

    How did you feel about this post?

    😍 🙂 😐 😕 😡

    Was this helpful?

    👍 👎